package com.swss.g.assets.action;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.swss.g.assets.bean.Asset;
import com.swss.g.assets.bean.AstRange;
import com.swss.g.assets.service.AssetCarService;
import com.swss.g.user.bean.User;
import com.swss.util.BeanUtils;
import com.swss.util.FileHelper;
import com.swss.util.Pagination;


public class AssetCarAction extends AssetAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1051331983437158041L;
    
	
	private List<?> clbzList;
	/**
	 * service
	 * 
	 * @return
	 */
	private AssetCarService assetCarService;
	/**
	 * bean
	 */
	private AstRange astRange;
	/**
	 * 新增前页面准备
	 * 
	 * @return
	 */
	public String si() {
		try {
			user = this.getSessionAttr("user", User.class);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			date = sdf.format(new Date());
			fundSubList = this.getSysService().getFundSub();
			useDirList = this.getSysService().getUseDir();
			nowStatusList = this.getSysService().getNowStatus();
			astSourceList = this.getSysService().getAstSource();
			chargeOffDeptList = this.getSysService().getChargeOffDept();
			byglList=this.getGbyglService().findAllStandField();
			zjfsList=this.getSysService().getZheJiuFangShi();
			clbzList=this.getSysService().getClbz();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "saveInit";
	}
	/**
	 * 保存车辆登记信息
	 */
	public String save() {
		try {
			String realPath = this.getRealPath("");
			if (picFileNames != null) {
				asset.setPicUrl1(FileHelper.saveFiles(pics, picFileNames,realPath));
			}
			if (billFileName != null) {
				asset.setPicUrl2(FileHelper.saveFile(bill, billFileName,realPath));
			}
			asset.setAstMark("Q");
			// 设置单据号
			if (asset.getDanJuNo() == null || asset.getDanJuNo().equals("")) {
				asset.setDanJuNo(this.currentPeriodService.getBillNo(this.astType));
			}
			this.assetService.addAsset(asset);
			this.print("{\"code\":0,\"billNo\":\"" + asset.getDanJuNo()+ "\"}");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 未审数据查询
	 */ 
	public String findAstNotAduit() {
		try {
			String deptNo = this.getSessionAttr("user", User.class).getDeptNo().trim();
			chargeOffDeptList = this.getSysService().getChargeOffDept();
			if (this.msg != null) {
				this.msg = new String(msg.getBytes("iso8859-1"), "UTF-8");
			}
			this.getAssetType("Q");
			if (this.code == null) {
				this.code = -1;
			}
			pagination = this.getAssetCarService().findAstByExample(pagination,asset,astRange,deptNo);
			pagination.setUrl("/swss" + this.getRequest().getServletPath());
			if (this.pagination.getRowcount() == 0)
				this.msg = "无满足条件的记录";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return INPUT;
		}
		return "resNotAduit";
	}
	/**
	 * 删除所选资产
	 * @param type=1:返回初审页面 否则返回登记管理
	 * @return firstAduitCmplt toResNotAduit
	 */ 
	public String deleteAll() {
		Integer type = Integer.parseInt(this.getRequest().getParameter("type"));
		try {
			auditNoArray = this.getRequest().getParameter("auditNoArray");
			if (auditNoArray != null) {
				this.code = this.assetCarService.astDeleteAll(auditNoArray);
				this.msg = URLEncoder.encode("删除成功", "utf-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
		if (type == 1)
			return "firstAduitCmplt";
		else
			return "toResNotAduit";
	}
	/**
	 * 修改前准备
	 * @param stamp 0.登记管理 1.资产初审 2.资产终审 
	 */ 
	public String modBefore() {
		try {
			fundSubList = this.getSysService().getFundSub();
			useDirList = this.getSysService().getUseDir();
			nowStatusList = this.getSysService().getNowStatus();
			astSourceList = this.getSysService().getAstSource();
			chargeOffDeptList = this.getSysService().getChargeOffDept();
			byglList = this.getGbyglService().findAllStandField();
			zjfsList = this.getSysService().getZheJiuFangShi();
			clbzList=this.getSysService().getClbz();
			stamp = Integer.parseInt(this.getRequest().getParameter("stamp"));
			asset = this.assetCarService.findOneById(asset.getId());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "modShow";
	}
	/**
	 *  修改数据
	 *  @param stamp 0.登记管理 1.资产初审 2.资产终审
	 */
	public String modAsset() {
		try {
			Asset a = this.getAssetCarService().get(asset.getId());
			String realPath = this.getRealPath("");
			// 修改单据图
			if (billFileName != null) {
				asset.setPicUrl2(FileHelper.saveFile(bill, billFileName,realPath));
			}
			// 删除部分图片
			String[] delPicUrls = this.delPic.split(",");
			for (String p : delPicUrls) {
				System.out.println(p.replaceAll("/", "\\\\"));
				FileHelper.deleteFile(realPath, p);
			}
			// 新增及修改图片
			if (picFileNames != null) {
				asset.setPicUrl1(asset.getPicUrl1() + ","
						+ FileHelper.saveFiles(pics, picFileNames, realPath));
			}
			// 计算 金额
			if (asset.getAstCount() > 1)
				asset.setAmount(asset.getPrice() * asset.getAstCount());
			else
				asset.setAmount(asset.getPrice());
			BeanUtils.copyProperties(asset, a);
			this.getAssetCarService().update(a);

			this.code = 0;
			this.msg = URLEncoder.encode("修改成功", "utf-8");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return INPUT;
		}
		if (stamp == 0)
			return "toResNotAduit";
		else if (stamp == 1)
			return "firstAduitCmplt";
		else
			return "lastAduitCmplt";
	}
	/**
	 * 初审数据查询
	 */ 
	@SuppressWarnings("unchecked")
	public String findAstFirstAduit() {
		String deptNo = this.getSessionAttr("user", User.class).getDeptNo().trim();
		try {
			if (this.msg != null) {
				this.msg = new String(msg.getBytes("iso8859-1"), "UTF-8");
			}
			chargeOffDeptList = this.getSysService().getChargeOffDept();
			this.getAssetType("Q");
			if (this.pagination == null) {
				pagination = new Pagination();
			}
			pagination = this.assetCarService.findAstFirstByExample(pagination,asset,astRange,deptNo);
			pagination.setUrl("/swss" + this.getRequest().getServletPath());

			if (this.pagination.getRowcount() == 0)
				this.msg = "无满足条件的记录";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return INPUT;
		}
		return "resFirstAduit";
	}
	/**
	 * 通过初审 状态为3 进入财务审核 财务审核失败状态变为2 修改后变为3 继续财务审核
	 * @param auditNoArray  前端接收过来的 asset ID
	 */ 
	public String goFirstAduit() {
		try {
			auditNoArray = this.getRequest().getParameter("auditNoArray");
			if (auditNoArray != null) {
				this.code = this.assetCarService.astFirstAuditUpdate(auditNoArray);
				this.msg = URLEncoder.encode("审核成功", "utf-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
		return "firstAduitCmplt";
	}
	public AssetCarService getAssetCarService() {
		return assetCarService;
	}

	public void setAssetCarService(AssetCarService assetCarService) {
		this.assetCarService = assetCarService;
	}
	public List<?> getClbzList() {
		return clbzList;
	}
	public void setClbzList(List<?> clbzList) {
		this.clbzList = clbzList;
	}
	
	public AstRange getAstRange() {
		return astRange;
	}
	public void setAstRange(AstRange astRange) {
		this.astRange = astRange;
	}
	/**
     * 判断asset对象是否为条件查询或默认查询内容
     * 对相关系统 设备，家具等设置标记 astmark S、Q等
     * @return
     */
    public Asset getAssetType(String astType) {
		// clearbean=1 清空session
		if (this.clearbean != null && this.clearbean == 1) {
			this.setSessionAttr("searchBean", null);
		  } else {
			if (asset != null) {
				this.setSessionAttr("searchBean", asset);
			} else {
				asset = this.getSessionAttr("searchBean", null);
			}
		}
		if (asset == null) {
			asset = new Asset();
			asset.setAstMark(astType);
		} else
			asset.setAstMark(astType);
		return asset;
	}
}
